package com.sql;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: sxlju_000
 * Date: 13-11-2
 * Time: 上午11:10
 * To change this template use File | Settings | File Templates.
 */
public class MySqlTypeHelper {
    private static Map<String, String> typeMap = new HashMap<String, String>();
    private static Map<String, Long> lengthMap = new HashMap<String, Long>();

    static {
        typeMap.put("java.lang.Integer", "int");
        typeMap.put("java.lang.Long", "bigint");
        typeMap.put("java.lang.Double", "double");
        typeMap.put("java.lang.String", "varchar");
        typeMap.put("java.generator.sql.Date", "datetime");
        typeMap.put("java.generator.sql.Timestamp", "timestamp");
    }

    static {
        lengthMap.put("java.lang.Integer", 0l);
        lengthMap.put("java.lang.Long", 0l);
        lengthMap.put("java.lang.Double", 0l);
        lengthMap.put("java.lang.String", 50l);
        lengthMap.put("java.generator.sql.Date", 0l);
        lengthMap.put("java.generator.sql.Timestamp", 0l);
    }

    public static String getMySqlType(String fieldType) {
        if (typeMap.get(fieldType) == null) {
            return "bigint";
        }
        return typeMap.get(fieldType);
    }

    public static Long getMySqlLength(String fieldType) {
        if (lengthMap.get(fieldType) == null) {
            return 0l;
        }
        return lengthMap.get(fieldType);
    }

    public static String getMySqlDefaultValue(Field field) {
        if (field.getName().equals("time")) {
//            return "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
            return "default current_timestamp on update current_timestamp";
        } else if (field.getType().getName().equals("java.generator.sql.Timestamp")) {
            return "";
        }
        return "default null";
    }

    public static String getMySqlForeignKeyName(Class<?> clazz, Field field) {

        return null;  //To change body of created methods use File | Settings | File Templates.
    }

}
